<?php
session_start();
require_once '../Model/M_AddNewUser.php';
require_once '../../Common/Model/M_Validation.php';
require_once '../../Common/Model/Helpper.php';
$log = new Helpper();
$log ->log("In add new user controller: C_AddNewUser.php", "True");
// Obtain user input from AddNewUser.php
$firstName = $_POST['first_name'];
$fatherName = $_POST['father_name'];
$familyName = $_POST['family_name'];
$username = $_POST['username'];
$password = $_POST['password'];
$re_password = $_POST['re_typed_password'];
$email = $_POST['email'];
//Initialize Com_Validation class
$validation = new Com_Validation();
//Validate user input
$isUsername = $validation->isUsername($username);
$isPassword = $validation->isPassword($password);
$isFirstName = $validation->isName($firstName);
$isFatherName = $validation->isName($fatherName);
$isFamilyName = $validation->isName($familyName);
$isEmail = $validation->isEmail($email);
$isMatchedPassword = $validation ->isMatchedPassword($password, $re_password);

if ($isUsername == false || $isPassword == false || $isFirstName == false ||
        $isFatherName == false || $isFamilyName == false || $isEmail == false ||
        $isMatchedPassword == false)
{
    $_SESSION['UserData']=false;
}
elseif ($isUsername == true && $isPassword == true && $isFirstName == true &&
         $isFatherName == true && $isFamilyName == true || $isEmail == true ||
         $isMatchedPassword == true)
{
    //Initialize U_User class
    $newUser = new U_User();
    //Insert a new user to DB
    mysql_query("SET NAMES 'utf8'");
    $newUser->inserUser($firstName, $fatherName, $familyName, $username, $password,
                      $email);
    //If inserting is done
    if($newUser->insertUser == true )
    {
      $_SESSION['UserData']=true;
    }
    //If inserting is not done
    else
    {
        $_SESSION['UserData']=false;
    }
}
//Redirect to AddNewUser.php page
header('Location:../View/AddNewUser.php');
?>
